package com.yxw.bamboo.core.database.sql;

import com.yxw.bamboo.api.dao.LogDbMapper;
import com.yxw.bamboo.core.NetworkContext;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

@Component
public class DbLog {

    private volatile boolean isSearching=false;
    private static final Queue<LogData> LOG_DATA_Q=new ConcurrentLinkedQueue<LogData>();

    @Autowired
    LogDbMapper logDbMapper;

    public void addLog(LogData logData)
    {
        LOG_DATA_Q.offer(logData);
    }


    public void addLog(Integer hostId, Integer socketHashcode, Integer socketStep,Integer offset, String logName, String logData)
    {
        LogData ld=new LogData( hostId,  socketHashcode,  socketStep, offset,  logName,  logData);
        LOG_DATA_Q.offer(ld);
    }

    public LogData poll(LogData logData)
    {
       return LOG_DATA_Q.poll();
    }

    public  void refresh()
    {
        if(LOG_DATA_Q.size()>0)
        {
//防止多个同时查询
            if(isSearching)
            {
                return;

            }
            isSearching=true;
            try{
                Integer id=0;
                if(Mock.MOCK_LIST.size()>0)
                {
                    LogData logData= LOG_DATA_Q.poll();
                    if(logData!=null)
                    {
                        logDbMapper.insert(logData);
                    }

                }
            }finally {
                isSearching=false;
            }


        }
    }


}
